﻿using Alos.Common;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Alos.Repository
{
    public class ReTestRepository : BaseRepository<ReTest>, IReTestRepository
    {
        /// <summary>
        /// 根据用户编号获取教材
        /// </summary>
        /// <param name="id"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public dynamic GetReTestByResouceId(int id)
        {
            var retest = DBContext.SingleOrDefault<dynamic>("select * from ReTest where ResourceID = @0", id);
            var stem = DBContext.Query<dynamic>("select * from ReTestStem where TestID in(select ID from ReTest where ResourceID = @0)", id).ToList();
            var opts = DBContext.Query<dynamic>("select * from ReTestOption where StemID in(select ID from ReTestStem where TestID in(select ID from ReTest where ResourceID = @0))", id).ToList();
            retest.stem = stem;
            foreach (var stm in stem)
            {
                stm.CorrectAnswer = DEncrypt.Encrypt(stm.CorrectAnswer);
                stm.opts = opts.FindAll(opt => opt.StemID == stm.ID);
            }
            return retest;
        }
        /// <summary>
        /// 根据用户编号获取教材
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReTest GetReTestById(int id)
        {
            return DBContext.SingleOrDefault<ReTest>("select * from ReTest where ResourceID = @0", id);
        }
    }
}
